package com.vietnam.power.domain;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProductGroup {

	private Integer id;
	private String name;
	private String description;
	private String keyword;
	private boolean status;
	private List<ProductGroup> subProductGroupList;
	private List<ProductGroup> parentProductGroupList;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<ProductGroup> getSubProductGroupList() {
		if (this.subProductGroupList == null) {
			this.subProductGroupList = new ArrayList<ProductGroup>();
		}
		return subProductGroupList;
	}

	public void setParentProductGroupList(List<ProductGroup> parentProductGroupList) {
		this.parentProductGroupList = parentProductGroupList;
	}

	public void addParentProductGroupList(ProductGroup parentProductGroupList) {
		getParentProductGroupList().add(parentProductGroupList);
	}
	
	public List<ProductGroup> getParentProductGroupList() {
		if (this.parentProductGroupList == null) {
			this.parentProductGroupList = new ArrayList<ProductGroup>();
		}
		Iterator<ProductGroup> itr = this.parentProductGroupList.iterator();
		while (itr.hasNext()) {
			ProductGroup pg = itr.next();
			if (pg.getId() == null) {
				itr.remove();
			}
		}
		return parentProductGroupList;
	}

	public void setSubProductGroupList(List<ProductGroup> subProductGroupList) {
		this.subProductGroupList = subProductGroupList;
	}

	public void addSubProductGroupList(ProductGroup subProductGroup) {
		getSubProductGroupList().add(subProductGroup);
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public void convertToPersist(com.vietnam.power.persitence.ProductGroup pg) {
		pg.setName(this.name);
		pg.setKeyword(this.keyword);
		pg.setDescription(this.description);
		pg.setStatus(this.status);
	}
	
	public void convertToDomain(com.vietnam.power.persitence.ProductGroup pgs) {
		ProductGroup pg = this;
		pg.setId(pgs.getId());
		pg.setName(pgs.getName());
		pg.setDescription(pgs.getDescription());
		pg.setKeyword(pgs.getKeyword());
		pg.setStatus(pgs.getStatus());
		for (com.vietnam.power.persitence.SubProductGroup spgs : pgs.getSubProductGroups2()) {
			ProductGroup child = new ProductGroup();
			child.setId(spgs.getProductGroup1().getId());
			child.setName(spgs.getProductGroup1().getName());
			child.setDescription(spgs.getProductGroup1().getDescription());

			pg.addSubProductGroupList(child);
		}
		
		for (com.vietnam.power.persitence.SubProductGroup spgs : pgs.getSubProductGroups1()) {
			ProductGroup parent = new ProductGroup();
			parent.setId(spgs.getProductGroup2().getId());
			parent.setName(spgs.getProductGroup2().getName());
			parent.setDescription(spgs.getProductGroup2().getDescription());

			pg.addParentProductGroupList(parent);
		}
	}
}
